package com.hp.gxw.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.gxw.entity.SysMenu;
import com.hp.gxw.mapper.SysMenuMapper;
import com.hp.gxw.service.ISysMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangping
 * @since 2020-03-04
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

	@Override
	public List<SysMenu> selectInfoList(Wrapper<SysMenu> ew) {
		return list(ew);
	}

	@Override
	public long selectInfoCount(Wrapper<SysMenu> ew) {
		return count(ew);
	}

	@Override
	@Transactional
	public void addInfo(SysMenu sysMenu, boolean autoBtn) throws Exception{
		save(sysMenu);
		if(autoBtn){
			String[] split = sysMenu.getMenuUrl().split("/");
			if(!split[split.length-1].equals("index")){
				List<SysMenu> btnList = new ArrayList<>();
				SysMenu view = new SysMenu();
				view.setId("btn:"+split[split.length-1]+"_view");
				view.setSupperId(sysMenu.getId());
				view.setMenuName("页面查看");
				view.setMenuType("按钮");
				btnList.add(view);
				SysMenu add = new SysMenu();
				add.setId("btn:"+split[split.length-1]+"_add");
				add.setSupperId(sysMenu.getId());
				add.setMenuName("新增（按钮）");
				add.setMenuType("按钮");
				btnList.add(add);
				SysMenu edit = new SysMenu();
				edit.setId("btn:"+split[split.length-1]+"_edit");
				edit.setSupperId(sysMenu.getId());
				edit.setMenuName("修改（按钮）");
				edit.setMenuType("按钮");
				btnList.add(edit);
				SysMenu delete = new SysMenu();
				delete.setId("btn:"+split[split.length-1]+"_delete");
				delete.setSupperId(sysMenu.getId());
				delete.setMenuName("删除（按钮）");
				delete.setMenuType("按钮");
				btnList.add(delete);
				SysMenu download = new SysMenu();
				download.setId("btn:"+split[split.length-1]+"_download");
				download.setSupperId(sysMenu.getId());
				download.setMenuName("导出（按钮）");
				download.setMenuType("按钮");
				btnList.add(download);
				SysMenu disable = new SysMenu();
				disable.setId("btn:"+split[split.length-1]+"_disable");
				disable.setSupperId(sysMenu.getId());
				disable.setMenuName("启用|禁用（按钮）");
				disable.setMenuType("按钮");
				btnList.add(disable);
				saveBatch(btnList);
			}
		}
	}

	@Override
	public SysMenu selectInfo(Wrapper<SysMenu> ew) {
		return getOne(ew);
	}

	@Override
	public void edit(SysMenu sysMenu) throws Exception{
		updateById(sysMenu);
	}
}